<?php
class CSS
{
	
}

class Field
{
	public $name;
	public $type;
	public $url;
	
	function __construct()
	{
		$this->type = 'text';
		//$this->url = '';
	}
}

class Column
{
	public $fields;
	public $caption;
	public $CSS;
	public $class;
	public $type;
	
	function __construct()
	{
		$this->CSS = new CSS();
		$this->type = 'text';
		$this->fields = array();
	}
}

class Helper_DataGrid
{
	public $class_name;
	public $table_css_class;
	public $array_object;
	public $PaginationHTML;
	public $array_field;
	public $url_add;
	
	public $checkbox_column;

	function __construct()
	{
		$this->table_css_class = "tableList";
		$this->checkbox_column = new Column();
		$this->checkbox_column->class = 'firstColumn';
		$this->checkbox_column->CSS->width = '40px';
		$this->checkbox_column->CSS->text_align = 'center';
		
		global $url_add;
		$this->url_add = $url_add;
	}
	
	private function get_style($obj_Column)
	{
		$style = '';
		$array_variable = get_object_vars($obj_Column->CSS);
		
		foreach( $array_variable as $key=>$val )
		{
			$style .= str_replace("_", "-", $key).': '.$val.'; ';
		}
		if( $style != '' ){
			$style = ' style="'.trim($style, "; ").'"';
		}
		return $style;
	} 
	function create()
	{
		parse_str($_SERVER['QUERY_STRING'], $query);
		
		if(isset($query['action'])) {
			unset($query['action']);
		}
		
		if( $this->url_add != '') {
			
		} else {
			$query_string = '';
			foreach($query as $key=>$val) {
				if( $query_string == '' ) {
					$query_string = $key.'='.$val;
					continue;
				}
				$query_string .= '&'.$key.'='.$val;
			}
			$this->url_add = 'index.php?action=add';
			
			if( $query_string != ''){
				$this->url_add .= '&'.$query_string;
			}
		}
	echo '
		<table class="'.$this->table_css_class.'">
			<tr class="operation">
				<td colspan="20">
					<a class="ui-button" href="'.$this->url_add.'" class="link_common_button_add">New</a>
					
					<select name="actionSelect" id="actionSelect" onchange="return submitForm();">
						<option value="">Selected Items</option>
						<option value="delete">Delete</option>
						<option value="lock">Lock</option>
						<option value="unlock">Unlock</option>
						<option value="copy">Copy</option>
					</select>
				</td>
			</tr>
			<tr class="table_list_header">
				<td class="'.$this->checkbox_column->class.'"'.$this->get_style($this->checkbox_column).'><input type="checkbox" name="cbHeaderChecked" id="cbHeaderChecked" onclick="return $.checkAll();" /></td>';
			
			foreach( $this->array_field as $obj_Column) {
				echo '
				<td'.$this->get_style($obj_Column).'>'.$obj_Column->caption.'</td>';
			}
		echo '
			</tr>';
		
		if(count($this->array_object)<=0)
		{
			echo '
				<tr>
					<td colspan="20">No records</td>
				</tr>
				';
		} else {
			$_rows_count = 0;
				
			foreach($this->array_object as $object) {
				echo '
			<tr class="row'.($_rows_count % 2).'">
				<td'.$this->get_style($this->checkbox_column).'><input type="checkbox" id="cbRowChecked_'.$object->id.'" name="cbRowChecked[]" value="'.$object->id.'" /></td>';
				foreach( $this->array_field as $obj_Column) {
					echo '
				<td'.$this->get_style($obj_Column).'>';
				
				$_fields_count = count($obj_Column->fields);
				$_fields_step = 0;
				foreach($obj_Column->fields as $obj_Field){
					if($obj_Field->type == 'text'){
						if( $obj_Field->name != '') {
							echo $object->{$obj_Field->name};
						} else {
							echo $obj_Field->text;
						}
					} else if($obj_Field->type == 'image'){
						preg_match_all('/\{(.*?)\}/', $obj_Field->src, $y);
						$src = $obj_Field->src;
						foreach($y[1] as $val) {
							$src = str_replace('{'.$val.'}', $object->{$val}, $src);
						}
						echo $src;
					} else {
						preg_match_all('/\{(.*?)\}/', $obj_Field->url, $y);
						$url = $obj_Field->url;
						foreach($y[1] as $val) {
							$url = str_replace('{'.$val.'}', $object->{$val}, $url);
						}
						
						if( $obj_Field->name != '') {
							echo '<a href="'.$url.'">'.$object->{$obj_Field->name}.'</a>';
						} else {
							echo '<a href="'.$url.'">'.$obj_Field->text.'</a>';
						}
					}
					if( $_fields_count > $_fields_step + 1){
						echo '&nbsp;';
					}
					$_fields_step++;
				}
				echo '</td>';
				}
				echo '
			</tr>
			';
				$_rows_count++;
			}
		
			if($this->pagination->CSS->display != 'none') {
				echo '
				<tr class="footer">
					<td colspan="20">'.$this->PaginationHTML.'</td>
				</tr>';
			}
		}
	echo '
			</table>

<script type="text/javascript">
	$(function() {
		$( ".ui-button" ).button();
		//$( "a.ui-button").click(function() { return false; });
	});
	function submitForm()
	{
		if( $("#actionSelect").val() == "") {
			$("#action").val("");
			return false;
		}
		
		if($.checkConfirmNumber($("#actionSelect option:selected").text(), 0)) {
			$("#action").val( $("#actionSelect").val() );
			$("#frmTableMain").submit();
		} else {
			$("#actionSelect").val("");
		}
	}
</script>';
	}
}
?>